package com.hk.core.data.jpa.domain;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hk.commons.util.JsonUtils;
import com.hk.core.data.jpa.annotations.SnowflakeIdGenerator;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Persistable;

import java.io.Serializable;

/**
 * 使用雪花id 算法生成id
 *
 * @author kevin
 * @date 2019-7-2 16:57
 */
@Getter
@Setter
@MappedSuperclass
@JsonFilter(value = JsonUtils.IGNORE_ENTITY_SERIALIZE_FIELD_FILTER_ID)
public abstract class AbstractSnowflakeIdPersistable implements Persistable<Long>, Serializable {

    /**
     * id 生成策略,strategy 的值可以为 {@link org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory} 中注册的key，也可以为 class 的全类名
     *
     * @see org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory
     */
    @Id
    @SnowflakeIdGenerator
    @Column(name = "id", updatable = false)
    private Long id;

    @Transient // DATAJPA-622
    @JsonIgnore
    @Override
    public final boolean isNew() {
        return null == getId();
    }

    @Override
    public String toString() {
        return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId());
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
